import java.util.HashMap;
import java.util.Arrays;
import java.util.Map;
class Solution {
    public int longestSubsequence(int[] arr, int difference) {
        int n=arr.length;
        Map<Integer,Integer> hash=new HashMap<Integer,Integer>();
        hash.put(arr[0],0);
        int ret=1;
        int[] dp=new int[n];
        Arrays.fill(dp,1);
        for(int i=1;i<n;i++){
            if(hash.get(arr[i]-difference)!=null){
                dp[i]=Math.max(dp[i],dp[hash.get(arr[i]-difference)]+1);
            }
            ret=Math.max(ret,dp[i]);
            hash.put(arr[i],i);
        }
        return ret;
    }
}